package com.ijava.base.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ijava.base.domain.Role;
import com.ijava.base.domain.User;
import com.ijava.base.service.RoleService;

@Controller
@RequestMapping("/admin/role.do")
@SessionAttributes("list")
public class RoleController {
	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private RoleService service;

	@RequestMapping(params = "action=list")
	public String list(ModelMap modelMap, Role role, HttpSession session)
			throws Exception {
		try {
			List list = service.findWithPaging(role);
			modelMap.addAttribute("list", list);
			modelMap.addAttribute("command", role);
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/role/roleList.jsp";
	}

	@RequestMapping(params = "action=new")
	public String neu(ModelMap modelMap, Role role, HttpSession session)
			throws Exception {
		try {
			modelMap.addAttribute("command", role);
		} catch (Exception e) {
			logger.error("", e);
		}
		return "forward:/admin/role/roleSaveEdit.jsp";
	}

	@RequestMapping(params = "action=edit")
	public String edit(ModelMap modelMap, Role role, HttpSession session)
			throws Exception {
		try {
			if (StringUtils.hasLength(role.getRoleId())) {
				modelMap.addAttribute("command", service.get(role.getRoleId()));
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("command", role);
		}
		return "forward:/admin/role/roleSaveEdit.jsp";
	}

	@RequestMapping(params = "action=remove")
	public String remove(ModelMap modelMap, String roleId, HttpSession session)
			throws Exception {
		modelMap.addAttribute("code", "role.remove.success");
		try {
			if(null != service.get(roleId)){
				service.remove(roleId);
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("code", "role.remove.failed");
			modelMap.addAttribute("isError", "true");
		}
		return list(modelMap, new Role(), session);
	}

	@RequestMapping(params = "action=mutilRemove")
	public String mutilRemove(ModelMap modelMap, Role role, HttpSession session)
			throws Exception {
		modelMap.addAttribute("code", "role.remove.success");
		try {
			service.mutilRemove(role);
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("code", "role.remove.failed");
			modelMap.addAttribute("isError", "true");
		}
		return list(modelMap, new Role(), session);
	}

	@RequestMapping(params = "action=saveEdit")
	public String saveEdit(ModelMap modelMap, Role role, HttpSession session)
			throws Exception {
		String roleId = role.getRoleId();
		try {
			User loginUser = (User) session.getAttribute("loginUser");
			
			if (roleId == null || roleId.length() == 0) {
				modelMap.addAttribute("code", "role.new.success");
				
				role.setCrUserId(loginUser.getUserId());
				role.setCrTime(new Date(System.currentTimeMillis()));
				service.save(role);

				if("saveAndNew".equals(role.getSaveAndNew())){
					return neu(modelMap, new Role(), session);
				} else {
					return edit(modelMap, role, session);
				}
			} else {
				modelMap.addAttribute("code", "role.modify.success");
				role.setUpUserId(loginUser.getUserId());
				role.setUpTime(new Date(System.currentTimeMillis()));
				service.update(role);
			}
		} catch (Exception e) {
			logger.error("", e);
			modelMap.addAttribute("isError", "true");
			if (roleId == null || roleId.length() == 0) {
				modelMap.addAttribute("code", "role.new.failed");
				modelMap.addAttribute("command", role);
				return neu(modelMap, role, session);
			} else {
				modelMap.addAttribute("code", "role.modify.failed");
				return edit(modelMap, role, session);
			}
		}
		return list(modelMap, new Role(), session);
	}
}
